package com.superservice.lya.wxapi;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

import com.fancy.learncenter.common.Constant;
import com.fancy.learncenter.common.MyApplication;
import com.fancy.learncenter.utils.LogUtil;
import com.fancy.learncenter.utils.ToastUtil;
import com.tencent.mm.sdk.constants.ConstantsAPI;
import com.tencent.mm.sdk.modelbase.BaseReq;
import com.tencent.mm.sdk.modelbase.BaseResp;
import com.tencent.mm.sdk.modelmsg.SendAuth;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.sdk.openapi.WXAPIFactory;

public class WXEntryActivity extends AppCompatActivity implements IWXAPIEventHandler {
    // IWXAPI 是第三方app和微信通信的openapi接口
    private IWXAPI api;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        api = WXAPIFactory.createWXAPI(this, Constant.APP_ID, false);
        api.handleIntent(getIntent(), this);
    }


    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }


    @Override
    public void onReq(BaseReq baseReq) {
        finish();
    }

    @Override
    public void onResp(BaseResp resp) {
//        COMMAND_SENDAUTH：认证请求
//        COMMAND_SENDMESSAGE_TO_WX：分享请求
        LogUtil.MyLog("WX_LOGIN_CODE", resp.getType() + "======resp.getType() ===" + MyApplication.WX_LOGIN_CODE);
        if (resp.getType() == ConstantsAPI.COMMAND_SENDAUTH) {
            Intent mIntent = new Intent(fancy.hyypaysdk.pay.Constant.WXPAY_CALLBACK_NAME);
            if (resp.errCode == 0) {
                MyApplication.WX_LOGIN_CODE = ((SendAuth.Resp) resp).code;
                LogUtil.MyLog("WX_LOGIN_CODE", "======WX_LOGIN_CODE===" + MyApplication.WX_LOGIN_CODE);
                mIntent.putExtra(fancy.hyypaysdk.pay.Constant.WXPAY_RESULT, "0");
            } else if (resp.errCode == -1) {
                mIntent.putExtra(fancy.hyypaysdk.pay.Constant.WXPAY_RESULT, "-1");
            } else if (resp.errCode == -2) {
                mIntent.putExtra(fancy.hyypaysdk.pay.Constant.WXPAY_RESULT, "-2");
            }
            sendBroadcast(mIntent);
        } else if (resp.getType() == ConstantsAPI.COMMAND_SENDMESSAGE_TO_WX) {
            if (resp.errCode == 0) {
                ToastUtil.show("分享成功");
            } else if (resp.errCode == -1) {
                ToastUtil.show("分享失败");
            } else if (resp.errCode == -2) {

            }
        }

        finish();
    }
}
